/**
 * @author mili
 */

// parse json file into parsedObject for use
	var json = (function () {
            var json = null;
            $.ajax({
                   'async': false,
                   'global': false,
                   'url': 'http://localhost/live-syd-traffic/traffic/traffic-data/incident.json',
                   'dataType': "json",
                   'success': function (data) {
                   json = data;
                   }
                   });
            return json;
            })();
            var type = json.type;
            var value = json["type"];
            alert(type);
            alert(value);
/**
 * parse the json file with the given url and data type
 * return a javascript object
 * 
 * @param {Object} url user specified file url
 * @param {Object} data type data type of the parsed json file, see CONSTANT.js
 * 
 * @return {Object}
 */
function parse(url, type){
	var json = (function(){
		var obj = null;
		$.ajax({
			'async':false,
			'global':false,
			'url':url,
			'dataType':"json",
			'success':function(data){
				obj = data;
			}
		});
		return obj;
	})();
	return json;
}

/**
 * Call to get the rights of given object
 * 
 * @param {Object} obj parsed javascript object
 * 
 * @return {Object}
 * */
function getRights(obj){
	return obj.rights;
}

/**
 * Call to get the copyright of the given rights
 * 
 * @param {Object} rights specified rights in the object
 * 
 * @return {String}
 * */
function getCopyRight(rights){
	return rights.copyright;
}

/**
 * Call to get the licence of the given rights
 * 
 * @param {Object} rights specified rights in the object
 * 
 * @return {String}
 * */
function getLicence(){
	return rights.licence;
}

/**
 * Call to get the layer name of the given object
 * 
 * @param {Object} obj specified javascript object
 * 
 * @return {String}
 * */
function getLayerName(obj){
	return obj.layerName;
}

/**
 * Call to get the last published time of the given javascript object
 * 
 * @param {Object} obj specified javascript object
 * 
 * @return {DateTime}
 * */
function getLastPublishedTime(obj){
	var unixDate = parseInt(obj.lastPublished);
	return new Date(unixDate);
}

/**
 * Below are simple/generic access object methods:
 * Generic attributes:
 * 		Type
 * 		id
 * 		geometry
 * 		properties --- different in each json files.
 */


/**
 * Call to get the features(array of feature) with given javascript object
 * 
 * @param {Object} obj user specified javascript object
 * 
 * @return {Array}
 * */
function getFeatures(obj){
	return obj.features;
}

/**
 * Get the given feature using index
 * 
 * @param {Array} features extracted features array
 * @param {Number} feature_index user specified index on the features
 * 
 * @return {Object}
 * */
function getFeature_index( features, feature_index){
	if(feature_index < 0 && feature_index >= features.length){
		return null;
	}
	return features[feature_index];
}

/**
 * Call to get the feature with the given unique feature id
 * 
 * @param {Array} features extracted features array
 * @param {Number} feature_id user specified feature id, use this id to locate feature in the features array, if not found, return null
 * 
 * @return {Object}
 * */
function getFeature_id( features, feature_id){
	for(var i = 0; i < features.length; i++){
		if(features[i].id == feature_id){
			return features[i];
		}
	}
	return null;
}

/**
 * Call to get the geometry type of the given feature
 * 
 * @param {Object} feature given feature
 * 
 * @return {String}
 * */
function getGeometryType(feature){
	return feature.type;
}

/**
 * Call to get the LatLng of the given feature --- a point with lat and lng (as an object)
 * 
 * @param {Object} feature given feature
 * 
 * @return {google.maps.LatLng}
 * */
function getLatLng(feature){
	var coordinates = feature.geometry.coordinates;
	var newLatLng = new google.maps.LatLng(coordinates[1],coordinates[0]);
	return newLatLng;
}

